<template>
  <div>
    <!-- 搜索框 -->
    <el-card id="search">
      <el-row>
        <el-col :span="20">
          <el-col :span="9">
            <el-select
              style="width: 80%"
              clearable
              v-model="searchModel.state"
              placeholder="请选择摊位状态"
            >
              <el-option
                v-for="item in stateList"
                :key="item.stateId"
                :label="item.stateName"
                :value="item.stateId"
              >
              </el-option>
            </el-select>
          </el-col>
          <el-button
            @click="getList"
            type="primary"
            round
            icon="el-icon-search"
          >查询</el-button
          >
        </el-col>
      </el-row>
    </el-card>
    <!-- 结果表格 -->
    <el-card>
      <el-table :data="dataList" stripe style="width: 100%">
        <el-table-column label="#" width="80">
          <template slot-scope="scope">
            <!-- (pageNo - 1) * pageSize + index + 1 -->
            {{
              (searchModel.pageNo - 1) * searchModel.pageSize + scope.$index + 1
            }}
          </template>
        </el-table-column>
        <el-table-column prop="id" label="摊位ID" width="120" align="center">
        </el-table-column>
        <el-table-column prop="boothName" label="摊位名称" width="180" align="center">
        </el-table-column>
        <el-table-column prop="area" label="摊位面积/m²" width="180" align="center">
        </el-table-column>
        <el-table-column prop="location" label="位置" width="180" align="center">
        </el-table-column>
        <el-table-column prop="rentalFee" label="租金/月(元)" width="120" align="center"> </el-table-column>
        <el-table-column prop="extraFee" label="水单杂费/月(元)" width="120" align="center"> </el-table-column>
        <el-table-column prop="description" label="描述" width="180" align="center"> </el-table-column>
        <el-table-column prop="nickName" label="摊主" width="180" align="center"> </el-table-column>
        <el-table-column prop="coverImages" label="摊位照片" width="220" align="center">
          <template slot-scope="scope">
            <el-image
              v-for="(coverImage, index) in scope.row.coverImages"
              :key="index"
              style="width: 100px; height: 100px; margin-right: 10px"
              :src="downloadImageUrl + coverImage"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="220" align="center">
          <template slot-scope="scope">
            <el-col>
              <el-button
                v-if="scope.row.state == 0"
                @click="editState(scope.row, 1)"
                type="success"
                round
              >审核通过</el-button>
              <el-button
                v-if="scope.row.state == 0"
                @click="editState(scope.row, -1)"
                type="danger"
                round
              >不允通过</el-button>
            </el-col>
            <el-button v-if="scope.row.state == 1" type="success" round>审核成功</el-button>
            <el-button v-if="scope.row.state == -1" type="danger" round>审核不过</el-button>
            <el-button v-if="scope.row.state == 2" type="primary" round>租赁中</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchModel.pageNo"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="searchModel.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

  </div>
</template>

<script>
import boothApi from "@/api/boothManage";
export default {
  data() {
    return {
      updateBooth: {},
      stateId: '',
      stateList: [
        {
          stateId: -1,
          stateName: '未通过'
        },
        {
          stateId: 0,
          stateName: '已提交'
        },
        {
          stateId: 1,
          stateName: '已通过'
        },
        {
          stateId: 2,
          stateName: '租赁中'
        },
      ],
      editorOption:{},
      imageUrl: '',
      uploadUrl: 'http://localhost:8080/common/upload',
      downloadImageUrl: 'http://localhost:8080/common/download?name=',
      formLabelWidth: "130px",
      form: {},
      dialogFormVisible: false,
      title: "",
      total: 0,
      searchModel: {
        pageNo: 1,
        pageSize: 10,
      },
      dataList: [],
    };
  },
  methods: {
    editState(row, id) {
      this.$confirm(`确定进行此操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.updateBooth.id = row.id
          this.updateBooth.state = id
          boothApi.save(this.updateBooth).then((response) => {
            // 成功提示
            this.$message({
              message: response.message,
              type: "success",
            });
            //刷新表格
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    clearForm() {
      this.form = {};
    },
    openEditUI(row) {
      if (row == null) {
        this.title = "新增新闻";
      } else {
        this.title = "修改新闻";
        this.form = row;
      }
      this.dialogFormVisible = true;
    },
    handleSizeChange(pageSize) {
      this.searchModel.pageSize = pageSize;
      this.getList();
    },
    handleCurrentChange(pageNo) {
      this.searchModel.pageNo = pageNo;
      this.getList();
    },
    beforeImageUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传图片只能是 JPG 格式或者是PNG!')
      }

      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    handleImageSuccess(response, file) {
      this.imageUrl =
        'http://localhost:8080/common/download?name=' + response.data
      this.form.coverImage = response.data
      console.log('上传的封面:',this.form.coverImage)
    },
    getList() {
      boothApi.getList(this.searchModel).then((response) => {
        this.dataList = response.data.rows;
        this.total = response.data.total;
      });
    },
  },
  created() {
    this.getList();
  },
};
</script>

<style>
#search .el-input {
  width: 200px;
  margin-right: 10px;
}
.el-dialog .el-input {
  width: 35%;
}
</style>
